import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { EdubaseAcademicYearApi } from '#/api/edu/base/setting/academicYear';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { formatDate } from '@vben/utils';

import { Typography } from 'ant-design-vue';

import { DictBadge } from '#/components/dict-badge';
import { DictTag } from '#/components/dict-tag';
import { getRangePickerDefaultProps } from '#/utils';

const { Paragraph } = Typography;
/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'academicYear',
      label: '学年',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'academicName',
      label: '学年名称',
      rules: 'required',
      component: 'Input',
      componentProps: {
        placeholder: '请输入学年名称',
      },
    },
    {
      fieldName: 'startDate',
      label: '开始日期',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'endDate',
      label: '结束日期',
      rules: 'required',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      rules: 'required',
      defaultValue: 0,
      component: 'RadioGroup',
      componentProps: {
        options: [
          {
            label: '未开始',
            value: 0,
          },
          {
            label: '进行中',
            value: 1,
          },
          {
            label: '已归档',
            value: 2,
          },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Textarea',
      componentProps: {
        rows: 4,
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '学年',
      field: 'academicYear',
    },
    {
      label: '学年名称',
      field: 'academicName',
    },
    {
      label: '开始日期',
      field: 'startDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '结束日期',
      field: 'endDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '状态',
      field: 'status',
      render: (val) => {
        return h(DictTag, {
          options: [
            {
              label: '未开始',
              value: 0,
            },
            {
              label: '进行中',
              value: 1,
            },
            {
              label: '已归档',
              value: 2,
            },
          ],
          value: val,
        });
      },
    },
    {
      label: '是否当前学年',
      field: 'isCurrent',
      render: (val) =>
        h(DictBadge, {
          value: val,
        }),
    },
    {
      label: '当前学期类型',
      field: 'currentSemesterType',
      show: (row) => row?.isCurrent,
      render: (val) =>
        h(DictTag, {
          type: DICT_TYPE.EDU_SEMESTER_TYPE,
          value: val,
        }),
    },
    {
      label: '备注',
      field: 'remark',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'academicYear',
      label: '学年',
      component: 'DatePicker',
      componentProps: {
        picker: 'year',
        format: 'YYYY',
        valueFormat: 'YYYY',
      },
    },
    {
      fieldName: 'academicName',
      label: '学年名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入学年名称',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: [
          {
            label: '未开始',
            value: 0,
          },
          {
            label: '进行中',
            value: 1,
          },
          {
            label: '已归档',
            value: 2,
          },
        ],
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<EdubaseAcademicYearApi.EduAcademicYear>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'academicYear',
      title: '学年',
      minWidth: 80,
    },
    {
      field: 'academicName',
      title: '学年名称',
      minWidth: 160,
    },
    {
      field: 'startDate',
      title: '开始日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'endDate',
      title: '结束日期',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: {
          options: [
            {
              label: '未开始',
              value: 0,
              colorType: 'warning',
            },
            {
              label: '进行中',
              value: 1,
              colorType: 'primary',
            },
            {
              label: '已归档',
              value: 2,
              colorType: 'info',
            },
          ],
        },
      },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 300,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 160,
      formatter: 'formatDateTime',
    },
    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
